home *** CD-ROM | disk | FTP | other *** search
- /*
- * DBMSsetup.c
- *
- * Data source setup dialog, and its associated functions.
- *
- * (c) Apple Computer, Inc 1993
- */
-
- #include <Controls.h>
- #include <Dialogs.h>
- #include <Events.h>
- #include <Lists.h>
- #include <Menus.h>
- #include <OSUtils.h>
- #include <Packages.h>
- #include <Quickdraw.h>
- #include <Resources.h>
- #include <String.h>
- #include <Strings.h>
- #include <TextEdit.h>
- #include <Types.h>
- #include <Windows.h>
- #include <TextUtils.h>
-
- #include "DialogUtilities.h"
- #include "ODBCINST.H"
- #include "SQL.H"
- #include "SampleSetup.h"
-
- #include "DBMSsetup.h"
-
- /*
- * Defines
- */
-
- #define kDBMSsetupDlogID 134
-
- #define iOKButton 1
- #define iCancelButton 2
- #define iDataSourceNameLabel 3
- #define iDescriptionLabel 4
- #define iOtherLabel 5
- #define iDataSourceName 6
- #define iDescription 7
- #define iOther 8
- #define iTranslationLabel 9
- #define iTranslationName 10
- #define iSelectButton 11
- #define iLine1 12
- #define iLine2 13
- #define iTitle 14
- #define iTitleUnderline 15
-
- /*
- * Structures
- */
-
- typedef struct
- {
- GrafPtr savePort;
- DialogPtr dialog;
- }
- DoDBMSsetupRec;
-
- /*
- * Defines
- */
-
- #define kStringTooLongAlertID 1000
-
- #define kMaxDescriptionLength 32
- #define kMaxOtherLength 32
-
- /*
- * Globals
- */
-
- static DoDBMSsetupRec gDoDBMSsetupRec;
- static char gTranslateDLL[256];
- static char gTranslate[256];
- static char gTranslateOption[256];
-
- /*
- * Prototypes
- */
-
- static Boolean Initialize (WindowPtr parentWindow, DataSourceInfo *info);
- static void Cleanup ();
- static Boolean DoOkButton (DataSourceInfo *info);
- static void DoDataSourceName ();
- static void DoDescription ();
- static void DoOther ();
- static void DoSelectButton ();
- static void AdjustItems ();
- static Boolean CheckNameFieldForValidity ();
- static Boolean CheckDescritptionFieldForValidity ();
- static Boolean CheckOtherFieldForValidity ();
- static void DoStringTooLongAlert (short iField, short iFieldLabel, short maxChars);
- static void NumToHexString (long num, char *ptr, long n);
- static void HexStringToNum (char *ptr, long *num);
-
- /*
- * Public functions
- */
-
- Boolean
- DoDBMSsetup(WindowPtr parentWindow, DataSourceInfo *info)
- {
- /*
- * Display the data source setup dialog, centered over parentWindow, and
- * handle events until the user dismisses the dialog.
- */
-
- Boolean result;
- Boolean done = false;
- short item;
-
- UPPInit( uppModalFilterProcInfo, StandardFilter )
-
- if (!Initialize(parentWindow, info)) return false;
-
- while (!done)
- {
- ModalDialog( UPP(StandardFilter), &item);
- switch (item)
- {
- case iOKButton:
- done = result = DoOkButton(info);
- break;
-
- case iCancelButton:
- result = false;
- done = true;
- break;
-
- case iDataSourceName:
- DoDataSourceName();
- break;
-
- case iDescription:
- DoDescription();
- break;
-
- case iOther:
- DoOther();
- break;
-
- case iSelectButton:
- DoSelectButton();
- break;
- }
- }
-
- Cleanup();
-
- return result;
- }
-
- /*
- * Private functions
- */
-
- UPPInitStatic(uppUserItemProcInfo,DrawItemLineGray)
- UPPInitStatic(uppUserItemProcInfo,DrawItemDoubleLine)
-
- static Boolean
- Initialize(WindowPtr parentWindow, DataSourceInfo *info)
- {
- /*
- * Create the dialog and initialize its items - return false if anything fails.
- */
-
- DialogPtr dialog = NULL;
- GrafPtr savePort = NULL;
-
- GetPort(&savePort);
- InitCursor();
- dialog = GetNewDialogOverWindow(kDBMSsetupDlogID, parentWindow);
- if (dialog == NULL) goto bail;
- SetPort(dialog);
-
- SetDUserItem(dialog, iLine1, (UniversalProcPtr)UPP(DrawItemLineGray) );
- SetDUserItem(dialog, iLine2, (UniversalProcPtr)UPP(DrawItemLineGray) );
- SetDUserItem(dialog, iTitleUnderline, (UniversalProcPtr)UPP(DrawItemDoubleLine) );
-
- c2pstr(info->name);
- SetDText(dialog, iDataSourceName, info->name);
- p2cstr(info->name);
-
- c2pstr(info->description);
- SetDText(dialog, iDescription, info->description);
- p2cstr(info->description);
-
- c2pstr(info->other);
- SetDText(dialog, iOther, info->other);
- p2cstr(info->other);
-
- strcpy(gTranslateDLL, info->translateDLL);
- strcpy(gTranslate, info->translate);
- strcpy(gTranslateOption, info->translateOption);
-
- gDoDBMSsetupRec.savePort = savePort;
- gDoDBMSsetupRec.dialog = dialog;
-
- AdjustItems();
- ShowWindow(dialog);
-
- return true;
-
- bail:
-
- if (dialog != NULL) DisposDialog(dialog);
- SetPort(savePort);
-
- return false;
- }
-
- static void
- Cleanup()
- {
- /*
- * Dispose of the dialog and its assundry allocations
- */
-
- DisposDialog(gDoDBMSsetupRec.dialog);
- SetPort(gDoDBMSsetupRec.savePort);
- }
-
- static Boolean
- DoOkButton(DataSourceInfo *info)
- {
- /*
- * Copy the dialog settings to the DataSourceInfo structure
- */
-
- if (!CheckNameFieldForValidity())
- return false;
- if (!CheckDescritptionFieldForValidity())
- return false;
- if (!CheckOtherFieldForValidity())
- return false;
-
- GetDText(gDoDBMSsetupRec.dialog, iDataSourceName, info->name);
- p2cstr(info->name);
- GetDText(gDoDBMSsetupRec.dialog, iDescription, info->description);
- p2cstr(info->description);
- GetDText(gDoDBMSsetupRec.dialog, iOther, info->other);
- p2cstr(info->other);
- strcpy(info->translate, gTranslate);
- strcpy(info->translateDLL, gTranslateDLL);
- strcpy(info->translateOption, gTranslateOption);
-
- return true;
- }
-
- static void
- DoDataSourceName()
- {
- /*
- * Handle changes to the "name" edit field
- */
-
- AdjustItems();
- }
-
- static void
- DoDescription()
- {
- /*
- * Handle changes to the "description" edit field
- */
-
- ;
- }
-
- static void
- DoOther()
- {
- /*
- * Handle changes to the "other" edit field
- */
-
- ;
- }
-
- static void
- DoSelectButton()
- {
- /*
- * Call the ODBC Configuration Manager to get the translator name and option.
- */
-
- char translate[256], translateDLL[256];
- UINT translateSize, translateDLLSize;
- DWORD option;
- long optionLong;
- Boolean success;
-
- strcpy(translate, gTranslateDLL);
- strcpy(translate, gTranslate);
- HexStringToNum(gTranslateOption, &optionLong);
- option = (DWORD) optionLong;
-
- success = (Boolean) SQLGetTranslator(gDoDBMSsetupRec.dialog, translate, 256, &translateSize,
- translateDLL, 256, &translateDLLSize, &option);
-
- if (success)
- {
- strcpy(gTranslateDLL, translateDLL);
- strcpy(gTranslate, translate);
- NumToHexString(option, gTranslateOption, 8);
-
- AdjustItems();
- }
- }
-
- static void
- AdjustItems()
- {
- /*
- * Enable/disable dialog items as appropriate
- */
-
- char name[256];
-
- GetDText(gDoDBMSsetupRec.dialog, iDataSourceName, name);
- EnableDItem(gDoDBMSsetupRec.dialog, iOKButton, (name[0] > 0));
- OutlineDItem(gDoDBMSsetupRec.dialog, iOKButton);
-
- c2pstr(gTranslate);
- SetDText(gDoDBMSsetupRec.dialog, iTranslationName, gTranslate);
- p2cstr(gTranslate);
- }
-
- static Boolean
- CheckNameFieldForValidity()
- {
- /*
- * Make sure the "Data Source Name" edit text field isn't too long.
- */
-
- char str[256];
-
- GetDText(gDoDBMSsetupRec.dialog, iDataSourceName, str);
- if (str[0] > SQL_MAX_DSN_LENGTH)
- {
- DoStringTooLongAlert(iDataSourceName, iDataSourceNameLabel, SQL_MAX_DSN_LENGTH);
- return false;
- }
-
- return true;
- }
-
- static Boolean
- CheckDescritptionFieldForValidity()
- {
- /*
- * Make sure the "Description" edit text field isn't too long.
- */
-
- char str[256];
-
- GetDText(gDoDBMSsetupRec.dialog, iDescription, str);
- if (str[0] > kMaxDescriptionLength)
- {
- DoStringTooLongAlert(iDescription, iDescriptionLabel, kMaxDescriptionLength);
- return false;
- }
-
- return true;
- }
-
- static Boolean
- CheckOtherFieldForValidity()
- {
- /*
- * Make sure the "Other" edit text field isn't too long.
- */
-
- char str[256];
-
- GetDText(gDoDBMSsetupRec.dialog, iOther, str);
- if (str[0] > kMaxOtherLength)
- {
- DoStringTooLongAlert(iOther, iOtherLabel, kMaxOtherLength);
- return false;
- }
-
- return true;
- }
-
- static void
- DoStringTooLongAlert(short iField, short iFieldLabel, short maxChars)
- {
- /*
- * Put up an alert that says something like ""Blather" cannot be longer
- * than 51 characters". Then when the user clicks OK hilite that field.
- */
-
- char label[256], num[256];
-
- GetDText(gDoDBMSsetupRec.dialog, iFieldLabel, label);
- if (label[*label] == ':')
- *label -= 1;
- NumToString(maxChars, num);
- ParamText(label, num, "", "");
- AlertOverWindow(kStringTooLongAlertID, NULL, gDoDBMSsetupRec.dialog);
- SelIText(gDoDBMSsetupRec.dialog, iField, 0, 32767);
- }
-
- static void
- NumToHexString(long num, char *str, long n)
- {
- char *digits = "0123456789ABCDEF";
-
- while (n-- > 0)
- *str++ = digits[(num >> (4*n)) & 0x000F];
-
- *str = '\0';
- }
-
- static void
- HexStringToNum(char *str, long *num)
- {
- char *p = str, ch;
- long v = 0;
-
- while (*p == ' ' || *p == '\t') p++;
-
- for (;;)
- {
- if (! (ch = *p++)) { p--; break; }
-
- if (ch >= '0' && ch <= '9') { v <<= 4; v += ch - '0'; }
- else if (ch >= 'a' && ch <= 'f') { v <<= 4; v += ch - 'a' + 10; }
- else if (ch >= 'A' && ch <= 'F') { v <<= 4; v += ch - 'A' + 10; }
- else break;
- }
-
- *num = v;
- }
-